package com.lecture.java8.time;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class RemoveTimeRanges {
    public static void main(String[] args) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 定义时间区间的起始时间和结束时间
        Date startTime = sdf.parse("2023-09-05 16:00:00");
        Date endTime = sdf.parse("2023-09-05 18:04:00");

        // 定义要去掉的时间段列表
        List<TimeInterval> removeIntervals = new ArrayList<>();
        removeIntervals.add(new TimeInterval(sdf.parse("2023-09-05 16:04:11"), sdf.parse("2023-09-05 16:10:30")));
        removeIntervals.add(new TimeInterval(sdf.parse("2023-09-05 16:24:11"), sdf.parse("2023-09-05 16:50:30")));

        // 获取剩余时间段
        List<TimeInterval> timeIntervals = removeTimeRanges(startTime, endTime, removeIntervals);

        // 打印剩余时间段
        for (TimeInterval interval : timeIntervals) {
            System.out.println("startTime=" + sdf.format(interval.getStartTime()) + " stopTime=" + sdf.format(interval.getEndTime()));
        }
    }

    // 表示时间段的类
    static class TimeInterval {
        private Date startTime;
        private Date endTime;

        public TimeInterval(Date startTime, Date endTime) {
            this.startTime = startTime;
            this.endTime = endTime;
        }

        public Date getStartTime() {
            return startTime;
        }

        public Date getEndTime() {
            return endTime;
        }
    }

    // 去掉多个时间段的方法
    private static List<TimeInterval> removeTimeRanges(Date startTime, Date endTime, List<TimeInterval> removeIntervals) {
        List<TimeInterval> timeIntervals = new ArrayList<>();
        timeIntervals.add(new TimeInterval(startTime, endTime));

        for (TimeInterval removeInterval : removeIntervals) {
            List<TimeInterval> newTimeIntervals = new ArrayList<>();
            for (TimeInterval interval : timeIntervals) {
                Date intervalStartTime = interval.getStartTime();
                Date intervalEndTime = interval.getEndTime();
                Date removeStartTime = removeInterval.getStartTime();
                Date removeEndTime = removeInterval.getEndTime();

                // 处理重叠的情况
                if (intervalEndTime.after(removeStartTime) && intervalStartTime.before(removeEndTime)) {
                    if (intervalStartTime.before(removeStartTime)) {
                        newTimeIntervals.add(new TimeInterval(intervalStartTime, removeStartTime));
                    }
                    if (intervalEndTime.after(removeEndTime)) {
                        newTimeIntervals.add(new TimeInterval(removeEndTime, intervalEndTime));
                    }
                } else {
                    newTimeIntervals.add(interval);
                }
            }
            timeIntervals = newTimeIntervals;
        }

        return timeIntervals;
    }
}